import Vue from 'vue'
import Vuex from 'vuex'
import Utils from '@/common/utils.js'

Vue.use(Vuex)

const storeData = {
  // userData: Utils.local('userData') ? (Utils.local('userData')) : '',
  // accessToken: Utils.local('accessToken') === '{}' ? '' : Utils.local('accessToken'),
  levelResult: Utils.session('levelResult') ? (Utils.session('levelResult')) : '',
  districtResult: Utils.session('districtResult') ? (Utils.session('districtResult')) : '',
  departmentResult: Utils.session('departmentResult') ? (Utils.session('departmentResult')) : ''
}

const state = {
  hasTabbar: false,
  // userData: storeData.userData,
  // accessToken: storeData.accessToken,
  levelResult: storeData.levelResult,
  districtResult: storeData.districtResult,
  departmentResult: storeData.departmentResult
}

const mutations = {
  // 是否显示tabber
  SET_TABBAR (state, val) {
    state.hasTabbar = val
  },
  // SET DATA
  SET_USERDATA (state, val) {
    Utils.local('userData', JSON.stringify(val))
    state.userData = val
  },
  SET_ACCESSTOKEN (state, val) {
    Utils.local('accessToken', val)
    // 设置全局token
    state.accessToken = val
  },
  SET_LEVEL (state, val) {
    Utils.session('levelResult', JSON.stringify(val))
    state.levelResult = val
  },
  SET_DISTRICT (state, val) {
    Utils.session('districtResult', JSON.stringify(val))
    state.districtResult = val
  },
  SET_DEPARTMENT (state, val) {
    Utils.session('departmentResult', JSON.stringify(val))
    state.departmentResult = val
  }
}

const store = new Vuex.Store({
  state,
  mutations
})
export default store
